package com.tss.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tss.mapper.QuestionMapper;
import com.tss.pojo.Question;
import com.tss.pojo.QuestionExample;
import com.tss.pojo.QuestionExample.Criteria;
import com.tss.pojo.Result;
import com.tss.service.QuestionService;
import com.tss.utils.BeanUtils;

@Service
public class QuestionServiceImpl implements QuestionService {

	@Autowired
	private QuestionMapper questionMapper;
	
	
	@Override
	public Result createQuestion(Question question) {
		Result result = new Result();
		if(question==null||question.getQuestionnaireId()==null||question.getContent()==null||question.getType()==null){
			result.setErrorMessage("参数错误！");
			return result;
		}
		question.setValidate(true);
		questionMapper.insert(question);
		
		return result.setSuccessMessage("添加问题成功！");
	}

	@Override
	public Result updateQuestion(Question question) {
		Result result = new Result();
		if(question==null||question.getId()==null){
			return result.setErrorMessage("参数错误！");
		}
		Question q = questionMapper.selectByPrimaryKey(question.getId());
		if(q==null){
			return result.setErrorMessage("问题不存在！");
		}
		q=BeanUtils.updateBean(q, question);
		questionMapper.updateByPrimaryKey(q);
		question.setType(q.getType());
		return result.setSuccessMessage("修改问题成功！");
	}

	@Override
	public Result deleteQuestion(Long id) {
		Result result = new Result();
		if(id==null){
			return result.setErrorMessage("参数错误！");
		}
		Question question = questionMapper.selectByPrimaryKey(id);
		if(question==null){
			return result.setErrorMessage("问题不存在！");
		}
		question.setValidate(false);
		questionMapper.updateByPrimaryKey(question);
		result.addResult("type", question.getType());
		return result.setSuccessMessage("删除问题成功！");
		
	}

	@Override
	public List<Question> listQuestion(Long questionnaireId) {
		if(questionnaireId==null){
			return null;
		}
		QuestionExample example = new QuestionExample();
		Criteria criteria = example.createCriteria();
		criteria.andValidateEqualTo(true);
		criteria.andQuestionnaireIdEqualTo(questionnaireId);
		List<Question> list = questionMapper.selectByExample(example);
		return list;
	}
	
	



}
